package cn.myjerry.io.others;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 数据类型(基本 + String)处理流
 * 1、输入流 DataInputStream
 * 2、输出流 DataOnputStream
 * @author zpon
 *
 */

public class DataDemo01 {
	public static void main(String[] args) throws IOException {
		//write("/Users/zpon/Desktop/test02.txt");
		read("/Users/zpon/Desktop/test02.txt");
	}
	/**
	 * 从文件中读取数据 + 类型
	 * @throws IOException 
	 */
	public static void read(String destPath) throws IOException {
		// 创建源
		File dest = new File(destPath);
		// 选择流
		DataInputStream dis = new DataInputStream(
				new BufferedInputStream(
						new FileInputStream(dest)
						)
				);
		// 读取与写出顺序必须一致 必须存在才能读取
		System.out.println(dis.readDouble());
		System.out.println(dis.readLong());
		System.out.println(dis.readUTF());
		dis.close();
	}
	
	/**
	 * 数据 + 类型 输出到文件
	 * @throws IOException 
	 */
	public static void write(String destPath) throws IOException {
		double porint = 2.5;
		long num = 100L;
		String str = "数据类型";
		
		// 创建源
		File dest = new File(destPath);
		// 选择流 DataOnputStream
		DataOutputStream dos = new DataOutputStream(
				new BufferedOutputStream(
						new FileOutputStream(dest)
						)
				);
		dos.writeDouble(porint);
		dos.writeLong(num);
		dos.writeUTF(str);
		dos.flush();
		
		dos.close();
	}
}
